library(lattice)
d <- read.csv("~/Desktop/wine/wine_results.csv")
d$wine <- factor(d$wine)
d$person <- factor(d$person)

# A quick summary
summary(d)
aggregate(d$rating, by=list(d$wine), median)
aggregate(d$rating, by=list(d$wine), mean)

# Start plotting to pdf
pdf("~/Desktop/wine/charts.pdf", width=8.5, height=11)

# plot the data by wine
print(bwplot(wine ~ rating, data=d, horizontal=T, main="Distribution of Ratings by Wine"))
#print(stripplot(wine ~ rating, data=d, horizontal=T))

# plot the data by person
print(bwplot(person ~ rating, data=d, horizontal=T,main="Distribution of Ratings by Person"))
#print(stripplot(person ~ rating, data=d, horizontal=T))

# by region
print(bwplot(place ~ rating, data=d, horizontal=T,main="Distribution of Ratings by Geographic Location"))
# by varietal
print(bwplot(varietal ~ rating, data=d, horizontal=T,main="Distribution of Ratings by Varietal/Blend"))

# Look at the distribution of ratings 
# to check for normality
densityplot(d$rating,main="Overall Distribution of Ratings",xlab="Rating")
qqnorm(d$rating)

oldpar <- par(mfrow=c(3,3))

# plot histogram for each wine
# gotta love functional programming
sapply( levels(d$wine), 
  function(x) { 
    wine.rating <- d$rating[d$wine==x]
    dens <- density(wine.rating)
    hist(wine.rating,xlim=c(0,10), ylim=c(0,0.4),breaks=c(0,2,4,6,8,10),xlab="Rating",ylab="Probability",probability=T, 
         main=paste(x, "\n", d$year[d$wine==x][1], d$maker[d$wine==x][1] ))
    lines(dens)
  } 
)

# plot histogram for each person 
par(oldpar)
oldpar <- par(mfrow=c(3,3))
sapply( levels(d$person), 
  function(x) { 
    wine.rating <- d$rating[d$person==x]
    dens <- density(wine.rating)
    hist(wine.rating,xlim=c(0,10), ylim=c(0,0.5),breaks=c(0,2,4,6,8,10),xlab="Rating",ylab="Probability",probability=T,
         main=paste("Wine Ratings By : ", x))
    lines(dens)
  } 
)

par(oldpar)
print(xyplot(d$rating ~ d$price, main="Overall Price vs. Rating",type=c("p","smooth"),span=2.0,ylab="Rating",xlab="Price"))

print(xyplot(d$rating ~ d$price|d$person, main="Price vs. Rating By Person",type=c("p","smooth"),span=2.0,ylab="Rating",xlab="Price"))
print(xyplot(d$rating ~ d$price|d$place, main="Price vs. Rating By Geographic Region",type=c("p","smooth"),span=2.0,ylab="Rating",xlab="Price"))
print(xyplot(d$rating ~ d$price|d$varietal, main="Price vs. Rating By Varietal/Blend",type=c("p","smooth"),span=2.0,ylab="Rating",xlab="Price"))

print(xyplot(d$rating ~ d$year,type=c("p","smooth"),span=2.0))

# Linear model
# Is there significant correlation between rating and year or price
ry.lm <- lm(d$rating ~ d$year)
rp.lm <- lm(d$rating ~ d$price)
summary(ry.lm)
summary(rp.lm)

# analysis of variance
# Is the variation in mean wine rating due to place or varietal
# ie did we actually find a difference between them?
d.place.aov <- aov(rating ~ place, data=d)
text(anova(d.place.aov))
summary.lm(d.place.aov)

d.var.aov <- aov(rating ~ varietal, data=d)
text(anova(d.var.aov))
summary.lm(d.var.aov)

dev.off()

